<?php include('header.php')?>
<?php
use \LaneWeChat\Core\Db;
use \LaneWeChat\Core\Ext;
$account_id = intval($_REQUEST['account_id']);

if (!$account_id) {
    exit("<script>alert('非法操作！');history.go(-1);</script>");

}

include '/var/www/html/wx/wechat/core/ext.lib.php';
include '/var/www/html/wx/wechat/core/db.lib.php';
$sql = "SELECT name,appid, appsecret,hash,token as wxtoken,encodingAESKey,fid,ac_type FROM pft_wx WHERE id=? AND fid=? LIMIT 1";
$stmt = Db::Connect()->prepare($sql);
$stmt->execute(array($account_id, $_SESSION['sid']));//array($account_id, $_SESSION['sid'])
$account = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$account) {
    exit("<script>alert('数据不存在，请先登录验证！');location.href='add_account_login.html';</script>");
}
if ($account['fid']!=$_SESSION['sid']) {
    $sql = "SELECT dname FROM pft_member WHERE id=? LIMIT 1";
    $stmt = Db::Connect()->prepare($sql);
    $stmt->execute(array($_SESSION['sid']));
    exit("<script>alert('该微信公众号已和票付通账号【".$stmt->fetchColumn(0)."】绑定，您无法重复绑定。');history.go(-1);</script>");
}
$url = Ext::GetApiUrl($_SESSION['sid'], $account['hash'], $account_id);
?>
	<title>公众号配置</title>
</head>
<body>
	<div class="navbar navbar-inverse navbar-static-top" role="navigation" style="position:static;">
		<div class="container-fluid">
			<ul class="nav navbar-nav">
				<li><a href="http://www.12301.cc"><i class="fa fa-reply-all"></i>返回系统</a></li>
			</ul>
		</div>
	</div>
	<div class="container-fluid">
		<div class="row">
            <?php include('leftbar.php')?>
            <div class="col-xs-12 col-sm-9 col-lg-10">
                <div class="clearfix">
                    <ul class="nav nav-tabs nav-width">
                        <li class="normal"><a href="javascript:;">1. 公众号验证</a></li>
                        <li class="active"><a href="javascript:;">2. 设置公众号信息</a></li>
                        <?php if($account['appid']):?>
                        <li class="normal"><a href="add_account_finish.html?account_id=<?=$account_id?>">3. 引导页面</a></li>
                        <?php else:?>
                        <li class="normal"><a href="javascript:;">3. 引导页面</a></li>
                        <?php endif;?>
                    </ul>
                    <form action="" method="post" class="form-horizontal" role="form" id="form1">
                        <input name="action" type="hidden" value="CreateAccount"/>
                        <input name="account_id" type="hidden" value="<?=$account_id?>"/>
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                设置公众号信息
                            </div>
                            <div class="panel-body">
                                <div class="form-group">
                                    <label class="col-xs-12 col-sm-3 col-md-2 control-label"><span style="color:red">*</span> 公众号名称</label>
                                    <div class="col-sm-9">
                                        <input type="text" name="name" class="form-control" value="<?=$account['name']?>" autocomplete="off">
                                        <span class="help-block">填写公众号的帐号名称</span>
                                    </div>
                                </div>
                                <div id="panel_1" style="display: block;">
                                    <div class="form-group">
                                        <label class="col-xs-12 col-sm-3 col-md-2 control-label">级别</label>
                                        <div class="col-sm-9">
                                            <label for="status_1" class="radio-inline"><input autocomplete="off" type="radio" name="ac_type" id="status_1" <?=$account['ac_type']==2?'checked':''?> value="2" onclick="$('#adv').hide();"> 普通订阅号</label>
                                            <label for="status_2" class="radio-inline"><input autocomplete="off" type="radio" name="ac_type" id="status_2" <?=$account['ac_type']==0?'checked':''?> value="0" onclick="$('#adv').show();"> 认证订阅号/普通服务号</label>
                                            <label for="status_3" class="radio-inline"><input autocomplete="off" type="radio" name="ac_type" id="status_3" <?=$account['ac_type']==1?'checked':''?> value="1" onclick="$('#adv').show();"> 认证服务号</label>
                                        </div>
                                    </div>
                                    <div id="adv" style="display: block;">
                                        <div class="form-group">
                                            <label class="col-xs-12 col-sm-3 col-md-2 control-label">AppId</label>
                                            <div class="col-sm-9">
                                                <input type="text" name="appid" class="form-control" value="<?=$account['appid']?>" autocomplete="off">
                                                <div class="help-block">请填写微信公众平台后台的AppId</div>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-xs-12 col-sm-3 col-md-2 control-label">AppSecret</label>
                                            <div class="col-sm-9">
                                                <input type="text"  name="appsecret" value="<?=$account['appsecret']?>" class="form-control" value="" autocomplete="off">
                                                <div class="help-block">请填写微信公众平台后台的AppSecret, 只有填写这两项才能管理自定义菜单</div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div id="panel_2" style="display: none;">
                                </div>
                                <div class="form-group">
                                    <label class="col-xs-12 col-sm-3 col-md-2 control-label" style="color:red">接口地址</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" value="<?=$url?>" readonly="readonly" autocomplete="off">
                                        <div class="help-block">设置“公众平台接口”配置信息中的接口地址</div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-xs-12 col-sm-3 col-md-2 control-label" style="color:red">Token</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" value="<?=$account['wxtoken']?>" readonly="readonly">
                                        <div class="help-block">与公众平台接入设置值一致，必须为英文或者数字，长度为3到32个字符. 请妥善保管, Token 泄露将可能被窃取或篡改平台的操作数据.</div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-xs-12 col-sm-3 col-md-2 control-label" style="color:red">EncodingAESKey</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" value="<?=$account['encodingAESKey']?>" readonly="readonly">
                                        <div class="help-block">与公众平台接入设置值一致，必须为英文或者数字，长度为43个字符. 请妥善保管, EncodingAESKey 泄露将可能被窃取或篡改平台的操作数据.</div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-12">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <input name="nextStep" type="button" value="下一步" class="btn btn-primary">
                                <input type="hidden" name="token" value="<?=$token?>">
                            </div>
                        </div>
                    </form>
                </div>
            </div>

		</div>
	</div>
	<div style="display:none" id="gMaskLayer" class="gMaskLayer"></div>
    <script>
        $(document).ready(function(){
            $("input[name=nextStep]").on('click', function(){
                var appid = $("input[name='appid']").val(),
                    appsecret  = $("input[name='appsecret']").val(),
                    name  = $("input[name='name']").val(),
                    ac_type  = $("input[name='ac_type']:checked").val();
                if (!name) {
                    alert('请输入公众号名称。');
                    $("input[name='name']").focus();
                    return false;
                }
                if (ac_type!=2) {
                    if (!appid) {
                        alert('请输入APPID。');
                        $("input[name='appid']").focus();
                        return false;
                    }
                    if (!appsecret) {
                        alert('请输入AppSecret。');
                        $("input[name='appsecret']").focus();
                        return false;
                    }
                }
                var param = {
                    action : 'CreateAccount',
                    account_id : $("input[name=account_id]").val(),
                    ac_type : ac_type,
                    appid : appid,
                    name : name,
                    appsecret : appsecret
                }
                $.post('ichat.php', param, function(ret){
//                    console.log(ret);
                    if (ret['status']=='ok') {
                        location.href = "add_account_finish.html?account_id=" +param['account_id'] ;
                    } else {
                        alert(ret['custom_msg']);
                    }
                }, 'json');
            })
        });
    </script>
	<?php include('footer.php')?>
</body>